package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.WlConfigPatsetting;
import com.ysd.lis.mapper.lab.WlConfigPatsettingMapper;
import com.ysd.lis.request.wl.WlConfigSettingParam;
import com.ysd.lis.service.lab.WlConfigPatsettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 病人信息配置 服务实现类
 * </p>
 *
 * @author WangHao
 * @since 2024-01-18
 */
@Service
public class WlConfigPatsettingServiceImpl extends ServiceImpl<WlConfigPatsettingMapper, WlConfigPatsetting> implements WlConfigPatsettingService {

    @Autowired
    private WlConfigPatsettingMapper configPatsettingMapper;

    @Override
    public Result findPatsettingByConfigId(WlConfigSettingParam wlConfigParam) {
        if (StringUtils.isBlank(wlConfigParam.getWLConfigId())) {
            return Result.fail("信息配置id不能为空");
        }
        LambdaQueryWrapper<WlConfigPatsetting> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WlConfigPatsetting::getDelFlag, 0);
        lambdaQueryWrapper.eq(WlConfigPatsetting::getWlConfigId, wlConfigParam.getWLConfigId());
        lambdaQueryWrapper.eq(Objects.nonNull(wlConfigParam.getIsDisabled()), WlConfigPatsetting::getIsDisabled, wlConfigParam.getIsDisabled());
        lambdaQueryWrapper.eq(Objects.nonNull(wlConfigParam.getIsVisiable()), WlConfigPatsetting::getIsVisiable, wlConfigParam.getIsVisiable());
        if (StringUtils.isNotBlank(wlConfigParam.getFilter())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(WlConfigPatsetting::getFieldCode, wlConfigParam.getFilter()).or().like(WlConfigPatsetting::getFieldName, wlConfigParam.getFilter()));
        }
        lambdaQueryWrapper.orderByAsc(WlConfigPatsetting::getDisplayOrder);
        List<WlConfigPatsetting> wlConfigPatsettings = configPatsettingMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", wlConfigPatsettings);
    }

    @Override
    public Result insertPatsettingByTemp(List<WlConfigPatsetting> configPatsetting) {
        if (StringUtils.isBlank(configPatsetting.get(0).getId())){
            //如果没有id,说明是通过模板直接新增的,WlConfigId绑定的历史数据进行删除
            LambdaQueryWrapper<WlConfigPatsetting> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(WlConfigPatsetting::getWlConfigId,configPatsetting.get(0).getWlConfigId());
            configPatsettingMapper.delete(lambdaQueryWrapper);
        }
        boolean b = this.saveOrUpdateBatch(configPatsetting);
        if(b){
            return Result.succ(1, "成功", configPatsetting);
        }else {
            return Result.fail(400,"失败","");
        }
    }

    @Override
    public Result insertOrUpdatePatsetting(List<WlConfigPatsetting> configPatsetting) {
        boolean b = this.saveOrUpdateBatch(configPatsetting);
        if(b){
            return Result.succ(1, "成功", null);
        }else {
            return Result.fail(400,"失败","");
        }
    }
}
